//---------------------------------------------------------------------------


#include "Boards.h"
	Boards::Boards(){
	}
	void Boards::initArea(){
		area = new int *[boardSize] ;
		cout<<endl<<"init area board = "<<boardSize;
		for( int i = 0 ; i < boardSize ; i++ ){
			area[i] = new int[boardSize];
		}
		for (int i=0;i<=boardMaxIdx;i++){
			for (int j=0;j<=boardMaxIdx;j++){
				area[i][j] = 0;
			}
		}
	}
	void Boards::initCoin(){
		myCoin = new Coin [coinMaxIdx+1] ;
		if (boardSize == 8){
		
			myCoin[0].setX(1);
			myCoin[0].setY(0);
			myCoin[1].setX(3);
			myCoin[1].setY(0);
			myCoin[2].setX(5);
			myCoin[2].setY(0);
			myCoin[3].setX(7);
			myCoin[3].setY(0);
			myCoin[4].setX(0);
			myCoin[4].setY(1);
			myCoin[5].setX(2);
			myCoin[5].setY(1);
			myCoin[6].setX(4);
			myCoin[6].setY(1);
			myCoin[7].setX(6);
			myCoin[7].setY(1);
			myCoin[8].setX(1);
			myCoin[8].setY(2);
			myCoin[9].setX(3);
			myCoin[9].setY(2);
			myCoin[10].setX(5);
			myCoin[10].setY(2);
			myCoin[11].setX(7);
			myCoin[11].setY(2);
			myCoin[12].setX(0);
			myCoin[12].setY(5);
			myCoin[13].setX(2);
			myCoin[13].setY(5);
			myCoin[14].setX(4);
			myCoin[14].setY(5);
			myCoin[15].setX(6);
			myCoin[15].setY(5);
			myCoin[16].setX(1);
			myCoin[16].setY(6);
			myCoin[17].setX(3);
			myCoin[17].setY(6);
			myCoin[18].setX(5);
			myCoin[18].setY(6);
			myCoin[19].setX(7);
			myCoin[19].setY(6);
			myCoin[20].setX(0);
			myCoin[20].setY(7);
			myCoin[21].setX(2);
			myCoin[21].setY(7);
			myCoin[22].setX(4);
			myCoin[22].setY(7);
			myCoin[23].setX(6);
			myCoin[23].setY(7);
		}
		else{
			myCoin[0].setX(1);
			myCoin[0].setY(0);
			myCoin[1].setX(3);
			myCoin[1].setY(0);
			myCoin[2].setX(5);
			myCoin[2].setY(0);
			myCoin[3].setX(7);
			myCoin[3].setY(0);
			myCoin[4].setX(9);
			myCoin[4].setY(0);
			myCoin[5].setX(0);
			myCoin[5].setY(1);
			myCoin[6].setX(2);
			myCoin[6].setY(1);
			myCoin[7].setX(4);
			myCoin[7].setY(1);
			myCoin[8].setX(6);
			myCoin[8].setY(1);
			myCoin[9].setX(8);
			myCoin[9].setY(1);
			myCoin[10].setX(1);
			myCoin[10].setY(2);
			myCoin[11].setX(3);
			myCoin[11].setY(2);
			myCoin[12].setX(5);
			myCoin[12].setY(2);
			myCoin[13].setX(7);
			myCoin[13].setY(2);
			myCoin[14].setX(9);
			myCoin[14].setY(2);			
			myCoin[15].setX(0);
			myCoin[15].setY(3);
			myCoin[16].setX(2);
			myCoin[16].setY(3);
			myCoin[17].setX(4);
			myCoin[17].setY(3);
			myCoin[18].setX(6);
			myCoin[18].setY(3);
			myCoin[19].setX(8);
			myCoin[19].setY(3);
			myCoin[20].setX(1);
			myCoin[20].setY(6);
			myCoin[21].setX(3);
			myCoin[21].setY(6);
			myCoin[22].setX(5);
			myCoin[22].setY(6);
			myCoin[23].setX(7);
			myCoin[23].setY(6);
			myCoin[24].setX(9);
			myCoin[24].setY(6);
			myCoin[25].setX(0);
			myCoin[25].setY(7);
			myCoin[26].setX(2);
			myCoin[26].setY(7);
			myCoin[27].setX(4);
			myCoin[27].setY(7);
			myCoin[28].setX(6);
			myCoin[28].setY(7);
			myCoin[29].setX(8);
			myCoin[29].setY(7);
			myCoin[30].setX(1);
			myCoin[30].setY(8);
			myCoin[31].setX(3);
			myCoin[31].setY(8);			
			myCoin[32].setX(5);
			myCoin[32].setY(8);
			myCoin[33].setX(7);
			myCoin[33].setY(8);
			myCoin[34].setX(9);
			myCoin[34].setY(8);
			myCoin[35].setX(0);
			myCoin[35].setY(9);
			myCoin[36].setX(2);
			myCoin[36].setY(9);				
			myCoin[37].setX(4);
			myCoin[37].setY(9);
			myCoin[38].setX(6);
			myCoin[38].setY(9);
			myCoin[39].setX(8);
			myCoin[39].setY(9);
		}
		
		for (int k =0; k<=coinMaxIdx; k++){
			myCoin[k].setNumber(k+1);
			myCoin[k].setStatus(2);			
			myCoin[k].setAlive(1);			
		}
		myCoin[3].setStatus(2);			
		myCoin[5].setStatus(2);			
		myCoin[9].setStatus(2);			
	}
	void Boards::fillArea(){
		initArea();
		for (int i=0;i<=coinMaxIdx;i++){
			if (myCoin[i].getAlive()==1){
				area[myCoin[i].getY()][myCoin[i].getX()] = myCoin[i].getNumber();
			}
			else{
				area[myCoin[i].getY()][myCoin[i].getX()] = 0;				
			}			
		}
	}
	void Boards::printCoin(){
		printArea(area);
		int virtualArea[8][8];	
		for (int i=0;i<=7;i++){
			for (int j=0;j<=7;j++){
				virtualArea[i][j] = 0;
			}
		}
		for (int i=0;i<=23;i++){
			if (myCoin[i].getAlive()==1){
				virtualArea[myCoin[i].getY()][myCoin[i].getX()] = myCoin[i].getNumber();
			}
		}
		cout<<"# # # # # # # # # #"<<endl;
		for (int i=0;i<=7;i++){
			cout<<endl<<"| ";
			for (int j=0;j<=7;j++){
				switch (virtualArea[i][j]){
					case 0:
						cout<<"- ";
						break;
					
					case 10:
						cout<<"O ";
						break;
					case 11:
						cout<<"! ";
						break;
					case 12:
						cout<<"@ ";
						break;
					case 13:
						cout<<"a ";
						break;
					case 14:
						cout<<"b ";
						break;
					case 15:
						cout<<"c ";
						break;
					case 16:
						cout<<"d ";
						break;
					case 17:
						cout<<"e ";
						break;
					case 18:
						cout<<"f ";
						break;
					case 19:
						cout<<"g ";
						break;
					case 20:
						cout<<"h ";
						break;
					case 21:
						cout<<"i ";
						break;
					case 22:
						cout<<"j ";
						break;
					case 23:
						cout<<"k ";
						break;
					case 24:
						cout<<"l ";
						break;
					default:
						cout<<virtualArea[i][j]<<" ";				
						break;
				}
			}
			cout<<"|"<<endl;
		}
		cout<<endl<<"# # # # # # # # # #"<<endl;
	}
	void Boards::printArea(int** mArea){
		cout<<"# # # # # # # # # #"<<endl;
		for (int i=0;i<=boardMaxIdx;i++){
			cout<<endl<<"| ";
			for (int j=0;j<=boardMaxIdx;j++){
				switch (mArea[i][j]){
					case 0:
						cout<<"- ";
						break;
					
					case 10:
						cout<<"O ";
						break;
					case 11:
						cout<<"! ";
						break;
					case 12:
						cout<<"@ ";
						break;
					case 13:
						cout<<"a ";
						break;
					case 14:
						cout<<"b ";
						break;
					case 15:
						cout<<"c ";
						break;
					case 16:
						cout<<"d ";
						break;
					case 17:
						cout<<"e ";
						break;
					case 18:
						cout<<"f ";
						break;
					case 19:
						cout<<"g ";
						break;
					case 20:
						cout<<"h ";
						break;
					case 21:
						cout<<"i ";
						break;
					case 22:
						cout<<"j ";
						break;
					case 23:
						cout<<"k ";
						break;
					case 24:
						cout<<"l ";
						break;
					default:
						cout<<mArea[i][j]<<" ";				
						break;
				}
			}
			cout<<"|"<<endl;
		}
		cout<<endl<<"# # # # # # # # # #"<<endl;

	}
	
	void Boards::playGame(){
		initGame();

        gameLoop();
	}
	void Boards::initGame(){
		// loadFile = "lastGame.dat";
		// saveFile = "lastGame.dat";
		// pauseGame = false;
		// bool flag;
		// int confirmLoad;
		// int modeLoad;
		// int turnLoad;			
		// bool enemyLoad;			
		// comLevel = 1;
		// initArea();
		// cout<<endl<<"continue game ? (1/0) = ";
		// cin>>confirmLoad;
		// if (confirmLoad==1){
			// mHistory.LoadLastGame(&flag, loadFile, myCoin, &modeLoad, &turnLoad, &enemyLoad);
		// }
		// else{
			// flag = false;
		// }
		// if (flag){
			// turn = turnLoad;
			// battleMode = modeLoad;
			// enemyFront = enemyLoad;
			// cout<<endl<<"berhasil load last game";
			// cout<<endl<<"coin Area = ";
			// for (int z=0;z<=23;z++){
			// cout<<endl<<myCoin[z].getAlive();			
			// }
		// }
		// else{
			// cout<<endl<<"tidak ada last game saved";
			// turn = 4;
			// battleMode = 3;
			// enemyFront = false;
			// initCoin();
		// }
			// endGame = false;
			// fillArea();
			// printArea();

	}
	void Boards::gameLoop(){
		// while (!endGame){
			// moveCoin();
			// refresh();
			// //fillArea();
			// printArea();
			// printCoin();
		// }

	}
	void Boards::initBoards(int boards){
		boardSize = boards;
		boardMinIdx = 0;
		coinMinIdx = 0;
		if (boardSize == 8){
			boardMaxIdx = 7;
			coinMaxIdx = 23;
			secondCoinStartIdx = 12;
		}
		else if (boardSize == 10){
			boardMaxIdx = 9;
			coinMaxIdx = 39;
			secondCoinStartIdx = 20;
		
		}
	}
	void Boards::moveCoin(){
		// int moveTo=0;
		
			// if ((isForceEnemy()) && (!enemyFront)){ // jika harus memakan lawan dan baru pertama kali
				// enemyFront = true; // penanda bahwa harus memakan lawan
				// currentCoin = 9999; // penanda bahwa baru pertama kali harus memakan lawan
				// cout<<endl<<"harus makan!!!";
			// }
			// if ((turn ==1)||(turn ==2)){
				// playerMovement(&moveTo);
			// }
			// else{
				// comAIMovement(&moveTo);
			// }
			
			// if (enemyFront){
				// passCoinTo(moveTo);
				// if (!stillForceEnemy(currentCoin)){ // jika sudah tidak bisa memakan lawan lagi
					// playerTurn(); // ganti pemain
					// enemyFront = false; // penanda dimatikan agar tidak memakan lawan lagi
				// }
				// else{
					// cout<<endl<<"harus makan lagi WOIII ga ganti TURN!!!";
				
				// }
			// }
			// else{
				// moveCoinTo(moveTo);
				// playerTurn(); // ganti pemain
			// }
		
	}
	void Boards::enemyBlast(int coin){
		int moveTo;
		if(checkArea(coin)!=0){
			cout<<endl<<"makan coin! Mempass lawan ke = ";
			cin>>moveTo;
			if (isPassAble(coin,moveTo)){
				switch(moveTo){
					case 1:
						passLeftUp(coin);
						break;
					case 2:
						passRightUp(coin);
						break;
					case 3:
						passRightDown(coin);
						break;
					case 4:
						passLeftDown(coin);
						break;								
				}
				if ((getCoin(coin).getY()==7) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				if (checkArea(coin)!=0){
					enemyBlast(coin);
					cout<<endl<<"masih ada lawan tuh";
				}
				else{
					//playerTurn();
				}

			}
			else{
				cout<<endl<<"Anda harus mempass lawan... "<<moveTo;
			}
		}
		else{
			cout<<endl<<"coin yang dipilih bukan coin yang sedang berhadapan dengan lawan";
		}
	}
	void Boards::moveCoinTo(int currentCoin, int moveTo){
		switch(moveTo){
			case 1:
				coinMoveLeftUp(currentCoin);
				break;
			case 2:
				coinMoveRightUp(currentCoin);
				break;
			case 3:
				coinMoveRightDown(currentCoin);
				break;
			case 4:
				coinMoveLeftDown(currentCoin);
				break;
		}
	}
	void Boards::passCoinTo(int currentCoin, int moveTo){
		switch(moveTo){
			case 1:
				passLeftUp(currentCoin);
				break;
			case 2:
				passRightUp(currentCoin);
				break;
			case 3:
				passRightDown(currentCoin);
				break;
			case 4:
				passLeftDown(currentCoin);
				break;
		}
	}	
	void Boards::passCoinTo(int** mArea, Coin* mCoin, int coin, int moveTo){
		cout<<endl<<"violat coin="<<coin<<"("<<mCoin[coin-1].getX()<<","<<mCoin[coin-1].getY()<<"), moveTo"<<moveTo;
		switch(moveTo){
			case 1:
				mArea[mCoin[coin-1].getY()][mCoin[coin-1].getX()] = 0;
				mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()-2] = mCoin[coin-1].getNumber();
				mCoin[(getCoinNumberFromArea(mArea, mCoin, mCoin[coin-1].getX()-1, mCoin[coin-1].getY()-1))-1].setAlive(0);
				mArea[mCoin[coin-1].getY()-1][mCoin[coin-1].getX()-1] = 0; // mati
				mCoin[coin-1].setX(mCoin[coin-1].getX()-2);
				mCoin[coin-1].setY(mCoin[coin-1].getY()-2);
				if ((mCoin[coin-1].getY()==boardMinIdx) && (mCoin[coin-1].getStatus()==1)){
					mCoin[coin-1].setStatus(2);
				}
				break;
			case 2:
				mArea[mCoin[coin-1].getY()][mCoin[coin-1].getX()] = 0;
				mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()+2] = mCoin[coin-1].getNumber();
				mCoin[(getCoinNumberFromArea(mArea, mCoin, mCoin[coin-1].getX()+1, mCoin[coin-1].getY()-1))-1].setAlive(0);
				mArea[mCoin[coin-1].getY()-1][mCoin[coin-1].getX()+1] = 0; // mati
				mCoin[coin-1].setX(mCoin[coin-1].getX()+2);
				mCoin[coin-1].setY(mCoin[coin-1].getY()-2);
				if ((mCoin[coin-1].getY()==boardMinIdx) && (mCoin[coin-1].getStatus()==1)){
					mCoin[coin-1].setStatus(2);
				}
				break;
			case 3:
				mArea[mCoin[coin-1].getY()][mCoin[coin-1].getX()] = 0;
				mArea[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()+2] = mCoin[coin-1].getNumber();
				mCoin[(getCoinNumberFromArea(mArea, mCoin, mCoin[coin-1].getX()+1, mCoin[coin-1].getY()+1))-1].setAlive(0);
				mArea[mCoin[coin-1].getY()+1][mCoin[coin-1].getX()+1] = 0; // mati
				mCoin[coin-1].setX(mCoin[coin-1].getX()+2);
				mCoin[coin-1].setY(mCoin[coin-1].getY()+2);
				if ((mCoin[coin-1].getY()==boardMaxIdx) && (mCoin[coin-1].getStatus()==1)){
					mCoin[coin-1].setStatus(2);
				}
				cout<<endl<<mCoin[coin-1].getX()<<","<<mCoin[coin-1].getY()<<"="<<mArea[mCoin[coin-1].getY()][mCoin[coin-1].getX()];
				break;
			case 4:
				mArea[mCoin[coin-1].getY()][mCoin[coin-1].getX()] = 0;
				mArea[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()-2] = mCoin[coin-1].getNumber();
				mCoin[(getCoinNumberFromArea(mArea, mCoin, mCoin[coin-1].getX()-1, mCoin[coin-1].getY()+1))-1].setAlive(0);
				mArea[mCoin[coin-1].getY()+1][mCoin[coin-1].getX()-1] = 0; // mati
				mCoin[coin-1].setX(mCoin[coin-1].getX()-2);
				mCoin[coin-1].setY(mCoin[coin-1].getY()+2);
				if ((mCoin[coin-1].getY()==boardMaxIdx) && (mCoin[coin-1].getStatus()==1)){
					mCoin[coin-1].setStatus(2);
				}
				break;
		}
	}
	void Boards::coinMoveLeftUp(int coin){
		//cout<<endl<<"posisi koin di "<<getCoin(coin).getX()<<","<<getCoin(coin).getY();
		if ((getCoin(coin).getX()>boardMinIdx) && (getCoin(coin).getY()>boardMinIdx)){ // jika posisinya tidak di paling kiri atau di paling atas papan
			if (!existCoinLeftUp(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()-1][getCoin(coin).getX()-1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()-1);
				myCoin[coin-1].setY(getCoin(coin).getY()-1);
					cout<<"melangkah dengan pasti";
				if ((getCoin(coin).getY()==boardMinIdx) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				//playerTurn();
			}
			else{
				//
				cout<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			cout<<"nabrak dinding";
		}
	}
	void Boards::coinMoveLeftDown(int coin){
		if ((getCoin(coin).getX()>boardMinIdx) && (getCoin(coin).getY()<boardMaxIdx)){ // jika posisinya tidak di paling kiri atau di paling bawah papan
			if (!existCoinLeftDown(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()+1][getCoin(coin).getX()-1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()-1);
				myCoin[coin-1].setY(getCoin(coin).getY()+1);
				if ((getCoin(coin).getY()==boardMaxIdx) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;

				}
				//cout<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				//cout<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			//cout<<"nabrak dinding";
		}
	}
	void Boards::coinMoveRightUp(int coin){
		if ((getCoin(coin).getX()<boardMaxIdx) && (getCoin(coin).getY()>boardMinIdx)){ // jika posisinya tidak di paling kanan atau di paling atas papan
			if (!existCoinRightUp(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()-1][getCoin(coin).getX()+1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()+1);
				myCoin[coin-1].setY(getCoin(coin).getY()-1);
				if ((getCoin(coin).getY()==boardMinIdx) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				//cout<<endl<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				//cout<<endl<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			//cout<<endl<<"nabrak dinding";
		}
	
	}
	void Boards::coinMoveRightDown(int coin){

		if ((getCoin(coin).getX()<boardMaxIdx) && (getCoin(coin).getY()<boardMaxIdx)){ // jika posisinya tidak di paling kanan atau di paling bawah papan
			if (!existCoinRightDown(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()+1][getCoin(coin).getX()+1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()+1);
				myCoin[coin-1].setY(getCoin(coin).getY()+1);
				if ((getCoin(coin).getY()==boardMaxIdx) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				//cout<<endl<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				//cout<<endl<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			//cout<<endl<<"nabrak dinding";
		}
	
	}
	bool Boards::existCoin(int** mArea, Coin* mCoin, int coin, int dir){
		bool empty = true;
		if (dir==1){
			if (mArea[mCoin[coin-1].getY()-1][mCoin[coin-1].getX()-1]==0){
				empty = false;
			}
		}
		else if(dir==2){
			if (mArea[mCoin[coin-1].getY()-1][mCoin[coin-1].getX()+1]==0){
				empty = false;
			}

		}
		else if (dir==3){
			if (mArea[mCoin[coin-1].getY()+1][mCoin[coin-1].getX()+1]==0){
				empty = false;
			}
		
		}
		else if (dir==4){
			if (mArea[mCoin[coin-1].getY()+1][mCoin[coin-1].getX()-1]==0){
				empty = false;
			}
		
		}

		return empty;
	}
	bool Boards::existCoinLeftUp(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()-1][getCoin(coin).getX()-1]==0){
			empty = false;
		}
		else{
			//cout<<endl<<"ada coin di kiri atas";
		}
		return empty;
	}	bool Boards::existCoinLeftDown(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()+1][getCoin(coin).getX()-1]==0){
			empty = false;
		}
		else{
			//cout<<endl<<"ada coin di kiri bawah";
		}
		return empty;
	
	}
	bool Boards::existCoinRightUp(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()-1][getCoin(coin).getX()+1]==0){
			empty = false;
		}
		else{
			//cout<<endl<<"ada coin di kanan atas";
		}
		return empty;
	
	}
	bool Boards::existCoinRightDown(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()+1][getCoin(coin).getX()+1]==0){
			empty = false;
		}
		else{
			//cout<<endl<<"ada coin di kanan bawah";
		}
		return empty;
	
	}
	bool Boards::passLeftUp(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()-2][getCoin(coin).getX()-2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()-1), getCoin(coin).getY()-1)-1].setAlive(0);
		area[getCoin(coin).getY()-1][getCoin(coin).getX()-1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()-2);
		myCoin[coin-1].setY(getCoin(coin).getY()-2);
		if ((getCoin(coin).getY()==boardMinIdx) && (getCoin(coin).getStatus()==1)){
			myCoin[coin-1].setStatus(2);
		}
		//	cout<<endl<<"melangkahi lawan ke kiri atas";
	}
	bool Boards::passRightUp(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()-2][getCoin(coin).getX()+2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()+1), getCoin(coin).getY()-1)-1].setAlive(0);
		area[getCoin(coin).getY()-1][getCoin(coin).getX()+1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()+2);
		myCoin[coin-1].setY(getCoin(coin).getY()-2);
		// cout<<endl<<"melangkahi lawan ke kanan atas";
		if ((getCoin(coin).getY()==boardMinIdx) && (getCoin(coin).getStatus()==1)){
			myCoin[coin-1].setStatus(2);
		}
	}
	bool Boards::passRightDown(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()+2][getCoin(coin).getX()+2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()+1), getCoin(coin).getY()+1)-1].setAlive(0);
		area[getCoin(coin).getY()+1][getCoin(coin).getX()+1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()+2);
		myCoin[coin-1].setY(getCoin(coin).getY()+2);
		// cout<<endl<<"melangkahi lawan ke kiri bawah";
		if ((getCoin(coin).getY()==boardMaxIdx) && (getCoin(coin).getStatus()==1)){
			myCoin[coin-1].setStatus(2);
		}	
	}
	bool Boards::passLeftDown(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()+2][getCoin(coin).getX()-2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()-1), getCoin(coin).getY()+1)-1].setAlive(0);
		area[getCoin(coin).getY()+1][getCoin(coin).getX()-1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()-2);
		myCoin[coin-1].setY(getCoin(coin).getY()+2);
		// cout<<endl<<"melangkahi lawan ke kanan bawah";
		if ((getCoin(coin).getY()==boardMaxIdx) && (getCoin(coin).getStatus()==1)){
			myCoin[coin-1].setStatus(2);
		}	
	}
	bool Boards::isLeftSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getX()==boardMinIdx){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isRightSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getX()==boardMaxIdx){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isUpSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getY()==boardMinIdx){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isDownSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getY()==boardMaxIdx){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	void Boards::refresh(){
			system("PAUSE");
		system("cls");
	}
	Coin Boards::getCoin(int coin){
		//std::cout<<endl<<"error di = "<<coin;
		return myCoin[coin-1];
	}
	int Boards::checkArea(int coin){
		int check = 0;
		if (getCoin(coin).getAlive()==1){ // jika bidak masih hidup
			if (((getCoin(coin).getX()-2)>=boardMinIdx) && ((getCoin(coin).getY()-2)>=boardMinIdx) && ((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
					if ((existCoinLeftUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()-1))) != getType(coin))){
						check = 1;
						//cout<<endl<<"ada coin musuh di kiri atas";
					}
			}
			if (((getCoin(coin).getX()+2)<=boardMaxIdx) && ((getCoin(coin).getY()-2)>=boardMinIdx)&&((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
					if ((existCoinRightUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()-1))) != getType(coin))){
						check = 2;
						//cout<<endl<<"ada coin musuh di kanan atas";
					}
				
			}
			if (((getCoin(coin).getX()+2)<=boardMaxIdx) && ((getCoin(coin).getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
					if ((existCoinRightDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()+1))) != getType(coin))){
						check = 3;
						//cout<<endl<<"ada coin musuh di kanan bawah";
					}
				
			}
			if (((getCoin(coin).getX()-2)>=boardMinIdx) && ((getCoin(coin).getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
					if ((existCoinLeftDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()+1))) != getType(coin))){
						check = 4;
						//cout<<endl<<"ada coin musuh di kiri bawah";
					}
				
			}
		}
		//cout<<"area = "<<check;
		return check;
	}
	int Boards::checkArea(int** mArea, Coin* mCoin, int coin){
		int check = 0;
		if (mCoin[coin-1].getAlive()==1){ // jika bidak masih hidup
			if (((mCoin[coin-1].getX()-2)>=boardMinIdx) && ((mCoin[coin-1].getY()-2)>=boardMinIdx) && ((getType(coin)==1)||(mCoin[coin-1].getStatus()==2))){
					if ((existCoin(mArea, mCoin, coin, 1)) && (mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()-2]==0) && (getType(getCoinNumberFromArea(mArea, mCoin, (mCoin[coin-1].getX()-1),(mCoin[coin-1].getY()-1))) != getType(coin))){
						check = 1;
						//cout<<endl<<"ada coin musuh di kiri atas";
					}
			}
			if (((mCoin[coin-1].getX()+2)<=boardMaxIdx) && ((mCoin[coin-1].getY()-2)>=boardMinIdx)&&((getType(coin)==1)||(mCoin[coin-1].getStatus()==2))){
					if ((existCoin(mArea, mCoin, coin, 2)) && (mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()+2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()+1),(mCoin[coin-1].getY()-1))) != getType(coin))){
						check = 2;
						//cout<<endl<<"ada coin musuh di kanan atas";
					}
				
			}
			if (((mCoin[coin-1].getX()+2)<=boardMaxIdx) && ((mCoin[coin-1].getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(mCoin[coin-1].getStatus()==2))){
					if ((existCoin(mArea, mCoin, coin, 3)) && (mArea[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()+2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()+1),(mCoin[coin-1].getY()+1))) != getType(coin))){
						check = 3;
						//cout<<endl<<"ada coin musuh di kanan bawah";
					}
				
			}
			if (((mCoin[coin-1].getX()-2)>=boardMinIdx) && ((mCoin[coin-1].getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(mCoin[coin-1].getStatus()==2))){
					if ((existCoin(mArea, mCoin, coin, 4)) && (area[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()-2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()-1),(mCoin[coin-1].getY()+1))) != getType(coin))){
						check = 4;
						//cout<<endl<<"ada coin musuh di kiri bawah";
					}
				
			}
		}
		//cout<<"area = "<<check;
		return check;
	}
	int Boards::getType(int coin){
		if ((coin>coinMinIdx) && (coin<=(secondCoinStartIdx))){
			return 2;
		}
		else if ((coin<=(coinMaxIdx+1)) && (coin>(secondCoinStartIdx))){
			return 1;
		}
		else {
			return 0;
		}
	}
	int Boards::getCoinNumberFromArea(int x, int y){
		int result = 0;
		if (((x>=boardMinIdx) && (x<=boardMaxIdx)) && ((y>=boardMinIdx) && (y<=boardMaxIdx))){
			result = area[y][x];
		}else{
			cout<<endl<<"coin error in "<<x<<","<<y;
		}
		return result;
	}
	int Boards::getCoinNumberFromArea(int** mArea, Coin* mCoin, int x, int y){
		int result = 0;
		if (((x>=boardMinIdx) && (x<=boardMaxIdx)) && ((y>=boardMinIdx) && (y<=boardMaxIdx))){
			result = mArea[y][x];
		}else{
			cout<<endl<<"from virtual area, coin error in "<<x<<","<<y;
		}
		return result;
	}
	bool Boards::isPassAble(int coin, int dir){
		bool check = false;
		if (((getCoin(coin).getX()-2)>=boardMinIdx) && ((getCoin(coin).getY()-2)>=boardMinIdx) && ((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()-1))) != getType(coin))){
					if (dir==1){
						check = true;
					}
				}
		}
		if (((getCoin(coin).getX()+2)<=boardMaxIdx) && ((getCoin(coin).getY()-2)>=boardMinIdx)&&((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()-1))) != getType(coin))){
					if (dir==2){
						check = true;
					}
				}
			
		}
		if (((getCoin(coin).getX()+2)<=boardMaxIdx) && ((getCoin(coin).getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()+1))) != getType(coin))){
					if (dir==3){
						check = true;
					}
				}
			
		}
		if (((getCoin(coin).getX()-2)>=boardMinIdx) && ((getCoin(coin).getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()+1))) != getType(coin))){
					if (dir==4){
						check = true;
					}
				}
			
		}
		return check;
	}
	bool Boards::isPassAble(int** mArea, Coin* mCoin, int coin, int dir){
		bool check = false;
		if (mCoin[coin-1].getAlive()==1){ // jika bidak masih hidup
			// cout<<endl<<"ada coin ="<<coin;
			if (dir==1){
				if (((mCoin[coin-1].getX()-2)>=boardMinIdx) && ((mCoin[coin-1].getY()-2)>=boardMinIdx) && ((getType(coin)==1)||(mCoin[coin-1].getStatus()==2))){
						if ((existCoin(mArea, mCoin, coin, 1)) && (mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()-2]==0) && (getType(getCoinNumberFromArea(mArea, mCoin, (mCoin[coin-1].getX()-1),(mCoin[coin-1].getY()-1))) != getType(coin))){
							check = true;
							// cout<<endl<<"ada coin musuh di kiri atas";
						}
				}
			}
			else if (dir==2){
				if (((mCoin[coin-1].getX()+2)<=boardMaxIdx) && ((mCoin[coin-1].getY()-2)>=boardMinIdx)&&((getType(coin)==1)||(mCoin[coin-1].getStatus()==2))){
						if ((existCoin(mArea, mCoin, coin, 2)) && (mArea[mCoin[coin-1].getY()-2][mCoin[coin-1].getX()+2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()+1),(mCoin[coin-1].getY()-1))) != getType(coin))){
							check = true;
							// cout<<endl<<"ada coin musuh di kanan atas";
						}
					
				}
			}
			else if (dir==3){
				if (((mCoin[coin-1].getX()+2)<=boardMaxIdx) && ((mCoin[coin-1].getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(mCoin[coin-1].getStatus()==2))){
						// cout<<endl<<"ada coin musuh di kanan bawah 1 ";
						if (existCoin(mArea, mCoin, coin, 3)){
							// cout<<endl<<"ada coin musuh di kanan bawah a ";
						
						}
						if (mArea[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()+2]==0){
							// cout<<endl<<"ada coin musuh di kanan bawah b ";
						}
						if (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()+1),(mCoin[coin-1].getY()+1))) != getType(coin)){
							// cout<<endl<<"ada coin musuh di kanan bawah c ";
						
						}
						if ((existCoin(mArea, mCoin, coin, 3)) && (mArea[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()+2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()+1),(mCoin[coin-1].getY()+1))) != getType(coin))){
							check = true;
							// cout<<endl<<"ada coin musuh di kanan bawah 2 ";
						}
					
				}
			
			}
			else if (dir==4){
				if (((mCoin[coin-1].getX()-2)>=boardMinIdx) && ((mCoin[coin-1].getY()+2)<=boardMaxIdx)&&((getType(coin)==2)||(mCoin[coin-1].getStatus()==2))){
						if ((existCoin(mArea, mCoin, coin, 4)) && (area[mCoin[coin-1].getY()+2][mCoin[coin-1].getX()-2]==0) && (getType(getCoinNumberFromArea((mArea, mCoin, mCoin[coin-1].getX()-1),(mCoin[coin-1].getY()+1))) != getType(coin))){
							check = true;
							// cout<<endl<<"ada coin musuh di kiri bawah";
						}
					
				}
			
			}
		}
		return check;
	}
	bool Boards::isPassAble(int coin){
		bool check = false;
		if((isPassAble(coin, 1)) || (isPassAble(coin,2)) || (isPassAble(coin,3)) || (isPassAble(coin,4))){
			check = true;
		}
		return check;
	}
	bool Boards::isPassAble(int** mArea, Coin* mCoin, int coin){
		bool check = false;
		if((isPassAble(mArea, mCoin, coin, 1)) || (isPassAble(mArea, mCoin, coin,2)) || (isPassAble(mArea, mCoin, coin,3)) || (isPassAble(mArea, mCoin, coin,4))){
			check = true;
		}
		return check;
	}
	bool Boards::safeMove(int coin){
		bool check = false;
		if((safeMove(coin, 1)) || (safeMove(coin,2)) || (safeMove(coin,3)) || (safeMove(coin,4))){
			check = true;
		}
		return check;
	}
	bool Boards::safeMove(int coin, int dir){
		bool check = false;
		int posX = getCoin(coin).getX();
		int posY = getCoin(coin).getY();
		int typeCoin = getType(coin);
		bool noCrossOpponent;
		bool noFrontOpponent;
		bool noBehindOpponent;
		bool betweenCoin;
		if ((dir==1)&& (ableToMove(coin, 1))){

			if (((getCoin(coin).getY()-1)==0) || ((getCoin(coin).getX()-1)==0)){ // bila coin berada di dekat sudut
				check  = true;
				// cout<<endl<<"udah di ujung papan, jadi safe";
			}
			else{
				if (((getCoin(coin).getX()-1)>0) && ((getCoin(coin).getY()-1)>0)){
					 noCrossOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX-2, posY-2)))||(area[posY-2][posX-2]==0)); 
					 noFrontOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX, posY-2))) || (area[posY-2][posX]==0));
					 noBehindOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX-2, posY))) ||((typeCoin!=getType(getCoinNumberFromArea(posX-2, posY))) && (getCoin(getCoinNumberFromArea(posX-2, posY)).getStatus()==1)) || (area[posY][posX-2]==0));
					 betweenCoin = ((area[posY][posX-2]!=0) && (area[posY-2][posX]!=0));
					if ((noCrossOpponent) && (((noFrontOpponent) && (noBehindOpponent)) || (betweenCoin))){
						check = true;
						// cout<<endl<<"safe untuk melangkah ke 1";
					}
				}
			}
		}
		if ((dir==2) && (ableToMove(coin, 2))){
			if (((getCoin(coin).getY()-1)==0) || ((getCoin(coin).getX()+1)==boardMaxIdx)){ // bila coin berada di dekat sudut
				check  = true;
				// cout<<endl<<"udah di ujung papan, jadi safe";
			}
			else{
				if (((getCoin(coin).getX()+1)<boardMaxIdx) && ((getCoin(coin).getY()-1)>0)){
					 noCrossOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX+2, posY-2)))||(area[posY+2][posX-2]==0)); 
					 noFrontOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX, posY-2))) || (area[posY-2][posX]==0));
					 noBehindOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX+2, posY))) ||((typeCoin!=getType(getCoinNumberFromArea(posX+2, posY))) && (getCoin(getCoinNumberFromArea(posX+2, posY)).getStatus()==1)) || (area[posY][posX+2]==0));
					 betweenCoin = ((area[posY][posX+2]!=0) && (area[posY-2][posX]!=0));
					if ((noCrossOpponent) && (((noFrontOpponent) && (noBehindOpponent)) || (betweenCoin))){
						check = true;
						// cout<<endl<<"safe untuk melangkah ke 2";
					}
				}
			}
		
		}
		if ((dir==3) && (ableToMove(coin, 3))){
			if (((getCoin(coin).getY()+1)==boardMaxIdx) || ((getCoin(coin).getX()+1)==boardMaxIdx)){ // bila coin berada di dekat sudut
				check  = true;
				// cout<<endl<<"udah di ujung papan, jadi safe";
			}
			else{
				if (((getCoin(coin).getX()+1)<boardMaxIdx) && ((getCoin(coin).getY()+1)<boardMaxIdx)){
					 noCrossOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX+2, posY+2)))||(area[posY+2][posX+2]==0)); 
					 if (noCrossOpponent)
						// cout<<endl<<"lulus cros";
					 noFrontOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX, posY+2))) || (area[posY+2][posX]==0));
					 if (noFrontOpponent)
						// cout<<endl<<"lulus front";
					 noBehindOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX+2, posY))) ||((typeCoin!=getType(getCoinNumberFromArea(posX+2, posY))) && (getCoin(getCoinNumberFromArea(posX+2, posY)).getStatus()==1)) || (area[posY][posX+2]==0));
					 if (noBehindOpponent)
						// cout<<endl<<"lulus behind";
					 betweenCoin = ((area[posY][posX+2]!=0) && (area[posY+2][posX]!=0));
					 if (betweenCoin)
						// cout<<endl<<"lulus between";
					if ((noCrossOpponent) && (((noFrontOpponent) && (noBehindOpponent)) || (betweenCoin))){
						check = true;
						// cout<<endl<<"safe untuk melangkah ke 3";
					}
				}
			}
		
		}
		if ((dir==4) && (ableToMove(coin, 4))){
			// cout<<endl<<"check nih "<<coin;
			if (((getCoin(coin).getY()+1)==boardMaxIdx) || ((getCoin(coin).getX()-1)==0)){ // bila coin berada di dekat sudut
				check  = true;
				// cout<<endl<<"udah di ujung papan, jadi safe";
			}
			else{
				if (((getCoin(coin).getX()-1)>0) && ((getCoin(coin).getY()+1)<boardMaxIdx)){
					 noCrossOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX-2, posY+2)))||(area[posY+2][posX-2]==0)); 
					 noFrontOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX, posY+2))) || (area[posY+2][posX]==0));
					 noBehindOpponent = ((typeCoin==getType(getCoinNumberFromArea(posX-2, posY))) ||((typeCoin!=getType(getCoinNumberFromArea(posX-2, posY))) && (getCoin(getCoinNumberFromArea(posX-2, posY)).getStatus()==1)) || (area[posY][posX-2]==0));
					 betweenCoin = ((area[posY][posX-2]!=0) && (area[posY+2][posX]!=0));
					if ((noCrossOpponent) && (((noFrontOpponent) && (noBehindOpponent)) || (betweenCoin))){
						check = true;
						// cout<<endl<<"safe untuk melangkah ke 4";
					}
				}
			}
		
		}
		
		
		return check;
	}
	bool Boards::mustEat(int coin){
		return isPassAble(coin);
	}
	bool Boards::isForceEnemy(int turn){
		bool face = false;
		//cout<<endl<<"giliran pemain = "<<turn;
		if ((turn ==1) || (turn == 4)){ // posisi pemain 1 dan 4 di bawah
			for (int k=secondCoinStartIdx; k<=coinMaxIdx; k++){
				if (myCoin[k].getAlive()==1){
					if (checkArea(k+1)!=0){
						face = true;
						//cout<<endl<<"ada coin yang berhadapan dengan lawan!!";
					}
					else{
						//cout<<endl<<"gagal di check area";

					}
				}
			}
		
		}
		else {
			for (int k=coinMinIdx; k<=(secondCoinStartIdx-1); k++){ // posisi pemain 2 dan 3 di atas
				if (myCoin[k].getAlive()==1){
					if (checkArea(k+1)!=0){
						face = true;
						//cout<<endl<<"ada coin yang berhadapan dengan lawan!!";
					}
					else{
						//cout<<endl<<"gagal di check area";

					}
				}
			}
		
		}
		return face;
	}
	bool Boards::stillForceEnemy(int coin){
		if (checkArea(coin)!=0){
			//cout<<endl<<"cek area false";
			return true;
		}
		else {
			//cout<<endl<<"cek area true";
			return false;
		}
	}
	int Boards::frontKiller(int turn){
		int firstCoin=9999;
	
		if ((turn ==1)||(turn ==4)){
			for (int i=secondCoinStartIdx;i<=coinMaxIdx;i++){
				if (getCoin(i+1).getAlive()==1){
					if (checkArea(i+1)!=0){
						if (firstCoin==9999){
							firstCoin = i+1;
							//cout<<endl<<"Kill one";
						}
						else{
							if (getCoin(i+1).getY()>getCoin(firstCoin).getY()){
								firstCoin = i+1;
								//cout<<endl<<"Kill More";
							}
						}
					}
				}
			}
		}
		else{
			for (int i=coinMinIdx;i<=(secondCoinStartIdx-1);i++){
				if (getCoin(i+1).getAlive()==1){
					if (checkArea(i+1)!=0){
						if (firstCoin==9999){
							firstCoin = i+1;
							//cout<<endl<<"Kill One";
						}
						else{
							if (getCoin(i+1).getY()>getCoin(firstCoin).getY()){
								firstCoin = i+1;
								//cout<<endl<<"Kill More";
							}
						}
					}
				}
			}
		
		}
		if (firstCoin==9999){
			cout<<endl<<"fungsi front Killer ERROR";
		}
		return firstCoin;
	}
	bool Boards::moreChanceToKill(int currentCoin){
		bool chance = 0;
		if (isPassAble(currentCoin,1)){
			chance += 1;
		}
		if (isPassAble(currentCoin,2)){
			chance += 1;
		}
		if (isPassAble(currentCoin,3)){
			chance += 1;
		}
		if (isPassAble(currentCoin,4)){
			chance += 1;
		}
		if (chance>1){
			return true;
		} else{
			return false;
		}
	}
	int Boards::frontPassKill(int currentCoin){
		int firstMove = checkArea(currentCoin);
		if (moreChanceToKill(currentCoin)){
			if (getType(currentCoin)==1){
				if (getCoin(currentCoin).getX()<4){
					if (isPassAble(currentCoin,1)){
						firstMove = 1;
					}
					else{
						if (isPassAble(currentCoin,2)){
							firstMove = 2;
						}
					}
				}
				else{
					if (isPassAble(currentCoin,2)){
						firstMove = 2;
					}
					else{
						if (isPassAble(currentCoin,1)){
							firstMove = 1;
						}
					}
				}
			}
			else{
				if (getCoin(currentCoin).getX()<4){
					if (isPassAble(currentCoin,3)){
						firstMove = 3;
					}
					else{
						if (isPassAble(currentCoin,4)){
							firstMove = 4;
						}
					}
				}
				else{
					if (isPassAble(currentCoin,4)){
						firstMove = 4;
					}
					else{
						if (isPassAble(currentCoin,3)){
							firstMove = 3;
						}
					}
				}
			
			}
		}
		return firstMove;
	}
	bool Boards::corner(int coin, int id){
		bool corner = false;
		switch(id){
			case 1:
				if ((getCoin(coin).getX()==boardMinIdx) || (getCoin(coin).getY()==boardMinIdx)){
					corner = true;
				}
				break;
			case 2:
				if ((getCoin(coin).getX()==boardMaxIdx) || (getCoin(coin).getY()==boardMinIdx)){
					corner = true;
				}
				break;
			case 3:
				if ((getCoin(coin).getX()==boardMaxIdx) || (getCoin(coin).getY()==boardMaxIdx)){
					corner = true;
				}
				break;
			case 4:
				if ((getCoin(coin).getX()==boardMinIdx) || (getCoin(coin).getY()==boardMaxIdx)){
					corner = true;
				}
				break;
		}
		return corner;
	}
	bool Boards::ableToMove(int coin){
		bool able = false;

			if (ableToMove(coin, 1)){
					able = true;
			//cout<<endl<<"coin ="<<coin<<"able to move 1";
			}
			else if (ableToMove(coin, 2)){
					able = true;
			//cout<<endl<<"coin ="<<coin<<"able to move 2";
				}
				else if (ableToMove(coin, 3)){
					able = true;
			//cout<<endl<<"coin ="<<coin<<"able to move 3";
				}
				else if (ableToMove(coin, 4)){
					able = true;
			//cout<<endl<<"coin ="<<coin<<"able to move 4";
				}
			if (able){
			//cout<<endl<<"able to move (1)";
			}
		return able;	
	}
	bool Boards::ableToMove(int coin, int side){
		bool able = false;
		switch(side){
			case 1:
				if (((getType(coin)==1)||(getCoin(coin).getStatus()==2))&& (!corner(coin, 1))&& (!existCoinLeftUp(coin)) ){
					if (getType(coin)==1){
						//cout<<endl<<"jebakan 1";
					}
					if (getCoin(coin).getStatus()==2){
						//cout<<endl<<"jebakan 2";
					}
					if (!existCoinLeftUp(coin)){
						//cout<<endl<<"jebakan 3";
					}
					if (!corner(coin, 1)){
						//cout<<endl<<"jebakan 4";
					}
					able = true;
				}
				break;
			case 2:
				if (((getType(coin)==1)||(getCoin(coin).getStatus()==2)) && (!corner(coin, 2)) && (!existCoinRightUp(coin))){
					able = true;
				}
				break;
			case 3:
				if (((getType(coin)==2)||(getCoin(coin).getStatus()==2))&& (!corner(coin, 3))&& (!existCoinRightDown(coin))){
					able = true;
				}
				break;
			case 4:
				if (((getType(coin)==2)||(getCoin(coin).getStatus()==2)) && (!corner(coin, 4))&& (!existCoinLeftDown(coin))){
					able = true;
				}
				break;
		}
		return able;
	}
	int Boards::anyWhereCanMove(int coin){
		int able=0;
		
			if (ableToMove(coin, 1)){
					able = 1;
			}
			else if (ableToMove(coin, 2)){
					able = 2;
				}
				else if (ableToMove(coin, 3)){
					able = 3;
				}else if (ableToMove(coin, 4)){
					able = 4;
				}
		if (able==0){
			cout<<endl<<"error nih anywhere can move nya";
		}
		return able;		
	}
	int Boards::sideCoin(int turn){
		int firstCoin=9999;
		int random_integer; 
		if ((turn ==1)||(turn ==4)){
			do{
				// srand((unsigned)time(0));
				random_integer = (rand()%secondCoinStartIdx)+1; 
			} while ((getCoin(random_integer+secondCoinStartIdx).getAlive()==0)||(!ableToMove(random_integer+secondCoinStartIdx)));
			firstCoin = random_integer+secondCoinStartIdx;
			cout<<endl<<"hasil random = "<<random_integer+secondCoinStartIdx;
				
			
			for (int i=secondCoinStartIdx;i<=coinMaxIdx;i++){
				if ((getCoin(i+1).getAlive()==1)&& (ableToMove(i+1))){
					//cout<<endl<<"i+1 = "<<(i+1);
					if (getCoin((i+1)).getAlive()==1){					
					//cout<<endl<<"hidup "<<(i+1);
					}
						if (firstCoin==9999){
							firstCoin = (i+1);
							cout<<endl<<"firstCoin Gan = "<<firstCoin;
						}
						else{
							if ((getCoin(i+1).getX()<(boardSize/2)) && (getCoin(i+1).getX()>boardMinIdx)){
								if (abs(getCoin(firstCoin).getX()-(boardSize/2))>abs(getCoin(i+1).getX())-(boardSize/2)){
									if (ableToMove(i+1)){ // bisa bergerak
										firstCoin = (i+1);
										cout<<endl<<"pergantian pemain nih Gan="<<firstCoin;
									}
									
								}
							}
							else if ((getCoin(i+1).getX()>=(boardSize/2)) && (getCoin(i+1).getX()<boardMaxIdx)){
								if (abs(getCoin(firstCoin).getX()-(boardSize/2))<abs(getCoin(i+1).getX()-(boardSize/2))){
									if (ableToMove(i+1)){ // bisa bergerak
										firstCoin = (i+1);
									}
									
								}
							}
						}					
				
				}
			}
		}
		else{
			do{
				// srand((unsigned)time(0));
				random_integer = (rand()%secondCoinStartIdx)+1; 
			} while ((getCoin(random_integer).getAlive()==0) || (!ableToMove(random_integer)));
			firstCoin = random_integer;
			cout<<endl<<"hasil random = "<<random_integer;
		
			for (int i=coinMinIdx;i<=(secondCoinStartIdx-1);i++){
				if ((getCoin(i+1).getAlive()==1)&& (ableToMove(i+1))){
						if (firstCoin==9999){
							firstCoin = (i+1);
						}
						else{
							if ((getCoin(i+1).getX()<(boardSize/2)) && (getCoin(i+1).getX()>boardMinIdx)){
								if (getCoin(firstCoin).getX()>getCoin(i+1).getX()){
									if (ableToMove(i+1)){ // bisa bergerak
										firstCoin = (i+1);
									}
									
								}
							}
							else if ((getCoin(i+1).getX()>=boardSize/2) && (getCoin(i+1).getX()<boardMaxIdx)){
								if (getCoin(firstCoin).getX()<getCoin(i+1).getX()){
									if (ableToMove(i+1)){ // bisa bergerak
										firstCoin = (i+1);
									}
									
								}
							}
						}					

				}
			}
		
		}
		if (firstCoin==9999){
			cout<<endl<<"fungsi side coin error";
		}
		else{
			cout<<endl<<"hasil fungsi = "<<firstCoin;
		
		}
		return firstCoin;
	}
	int Boards::sideMove(int currentCoin){
		int firstMove = anyWhereCanMove(currentCoin);
		cout<<endl<<"first move = "<<firstMove;
		if (moreChanceToMove(currentCoin)){
			cout<<endl<<"banyak pilihan gerak";
			if (getType(currentCoin)==1){
				if (getCoin(currentCoin).getX()<(boardSize/2)){
					if (ableToMove(currentCoin,1)){
						firstMove = 1;
						cout<<endl<<"bisa gerak ke 1";
					}
				}
				else{
					if (ableToMove(currentCoin,2)){
						firstMove = 2;
						cout<<endl<<"bisa gerak ke 2";
					}
				}
			}
			else{
				if (getCoin(currentCoin).getX()<(boardSize/2)){
					if (ableToMove(currentCoin,4)){
						firstMove = 4;
						cout<<endl<<"bisa gerak ke 4";
					}
				}
				else{
					if (ableToMove(currentCoin,3)){
						firstMove = 3;
						cout<<endl<<"bisa gerak ke 3";
					}
				}
			
			}
		}
		return firstMove;
	}
	int Boards::checkArea(int x, int y){
		return (checkArea(getCoinNumberFromArea(x,y)));
	}
	int Boards::killOneByOne(int**mArea, Coin* mCoin, int coin, int jabatan){
		int kill1 = 0;
		int kill2 = 0;
		int kill3 = 0;
		int kill4 = 0;
		int choice = 0;
		if (jabatan==1){ // bukan raja
			if (getType(coin)==1){ // jika bidak red
				if ((mCoin[coin-1].getY()==0) || (!isPassAble(mArea, mCoin, coin))){ // jika sudah di ujung atas papan atau sudah tidak bisa makan lagi
					return 0;
				}	
				else{ //ulang rekursi
					if (isPassAble(mArea, mCoin, coin, 1)){
						passCoinTo(mArea, mCoin, coin, 1);
						kill1 = 1 + killOneByOne(mArea, mCoin, coin, jabatan);
						
					}
					
					if (isPassAble(mArea, mCoin, coin, 2)){
						passCoinTo(mArea, mCoin, coin, 2);
						kill2 = 1+killOneByOne(mArea, mCoin, coin, jabatan);
					}
					if (kill1>kill2){
						choice = kill1;
					}
					else{
						choice = kill2;
					}
					return (choice+killOneByOne(mArea, mCoin, coin, jabatan));
				}
			}
			else{ // jika bidak black
				if ((mCoin[coin-1].getY()==boardMaxIdx) || (!isPassAble(mArea, mCoin, coin))){ // jika sudah di ujung atas papan atau sudah tidak bisa makan lagi
					return 0;
				}	
				else{ //ulang rekursi
					if (isPassAble(mArea, mCoin, coin, 3)){
						passCoinTo(mArea, mCoin, coin, 3);
						kill1 = 1 + killOneByOne(mArea, mCoin, coin, jabatan);
						
					}
					
					if (isPassAble(mArea, mCoin, coin, 4)){
						passCoinTo(mArea, mCoin, coin, 4);
						kill2 = 1+killOneByOne(mArea, mCoin, coin, jabatan);
					}
					if (kill1>kill2){
						choice = kill1;
					}
					else{
						choice = kill2;
					}
					return (choice+killOneByOne(mArea, mCoin, coin, jabatan));
				}
			
			}
		}
		else { // raja
			if (!isPassAble(mArea, mCoin, coin)){ // jika sudah di ujung atas papan atau sudah tidak bisa makan lagi
				return 0;
			}	
			else{ //ulang rekursi
				if (isPassAble(mArea, mCoin, coin, 1)){
					passCoinTo(mArea, mCoin, coin, 1);
					kill1 = 1 + killOneByOne(mArea, mCoin, coin, jabatan);
					
				}
				
				if (isPassAble(mArea, mCoin, coin, 2)){
					passCoinTo(mArea, mCoin, coin, 2);
					kill2 = 1+killOneByOne(mArea, mCoin, coin, jabatan);
				}
				if (isPassAble(mArea, mCoin, coin, 1)){
					passCoinTo(mArea, mCoin, coin, 1);
					kill3 = 1 + killOneByOne(mArea, mCoin, coin, jabatan);
					
				}
				
				if (isPassAble(mArea, mCoin, coin, 2)){
					passCoinTo(mArea, mCoin, coin, 2);
					kill4 = 1+killOneByOne(mArea, mCoin, coin, jabatan);
				}
				if ((kill1>=kill2) && (kill1>=kill3) && (kill1>=kill4)){
					choice = kill1;
				}
				else if ((kill2>=kill1) && (kill2>=kill3) && (kill2>=kill4)){
					choice = kill2;
				}
				else if ((kill3>=kill1) && (kill3>=kill2) && (kill3>=kill4)){
					choice = kill3;
				}
				else if ((kill4>=kill1) && (kill4>=kill3) && (kill4>=kill2)){
					choice = kill4;
				}
				return (choice+killOneByOne(mArea, mCoin, coin, jabatan));
			}		
		}
	}
	int Boards::howStrongToKill(int coin){
		int strong =0;
		int **virtualArea;
		Coin *virtualCoin;
		
		virtualArea = copyVirtualArea();
		virtualCoin = copyVirtualCoin();
		strong= killOneByOne(virtualArea, virtualCoin, coin, virtualCoin[coin-1].getStatus());
		return strong;
	}
	int ** Boards::copyVirtualArea() {
		int ** matrix;
		matrix = (int **) calloc(boardMaxIdx+1, sizeof(int *));
		for(unsigned int i = 0; i <= boardMaxIdx; i++){
			matrix[i] = (int *) calloc(boardMaxIdx+1, sizeof(int));
		}
		for (int i=0;i<=boardMaxIdx;i++){
			for (int j=0;j<=boardMaxIdx;j++){
				matrix[i][j] = area[i][j];
				// cout<<endl<<matrix[i][j];
			}
		}

		return matrix;
	}	
	Coin * Boards::copyVirtualCoin() {
		Coin * matrix;
		matrix = (Coin *) calloc(coinMaxIdx+1, sizeof(Coin));

		for (int i=0;i<=coinMaxIdx;i++){
				matrix[i] = myCoin[i];
				// cout<<endl<<"coin="<<matrix[i].getX();
		}

		return matrix;
	}
	// void Boards::copyVirtualArea(int *mVirtualArea[]){
		// mVirtualArea = new int[boardMaxIdx][boardMaxIdx];
		// for (int a=0;a<=boardMaxIdx;a++){
			// mVirtualArea[a] = new int [boardMaxIdx];
		// }
		// for (int i=0;i<=boardMaxIdx;i++){
			// for (int j=0;j<=boardMaxIdx;j++){
				// mVirtualArea[i][j] = 0;
			// }
		// }
		// mVirtualArea[4][5] = 5;
	// }
	int Boards::coinMostKill(int turn){
		int strong=0;
		int choice=0;
		int num = 0;
		int* candidate; // for candidate function
		int startFor, endFor; // for iteration

		if ((turn ==1)||(turn ==4)){ // if coin is red
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
			
		}
		else{ // if coin is black
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		// init candidate function
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) &&(checkArea(i+1)!=0)){
				num+=1;
			}
		}
		// init size of candidate
		candidate = new int[num];
		for (int k=0;k<num;k++){
			candidate[k] = 0;
		}		
		// re-init value of candidate
		num=0;
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) &&(checkArea(i+1)!=0)){
				candidate[num] = i+1; // assign value of candidate
				num+=1;
			}
		}
		// candidate coin is ready
		// std::cout<<endl<<"num"<<num;

		strong = howStrongToKill(candidate[0]);
		choice = candidate[0];
		if (num>1){ // jika banyak pilihan 
			int next;
			for (int z=1;z<num;z++){ // iterasi sebanyak jumlah candidate
				next = howStrongToKill(candidate[z]);
				if (strong<next){
					strong = next;
					choice = candidate[z];
				}
			}
		}
		else{
			// std::cout<<endl<<"cuma satu yang bisa makan";
		}
		// std::cout<<endl<<"hasil fungsi coin yang paling banyak makan = "<<choice;
		if (choice ==0){
			cout<<"coin most kill error";
		}
		return choice;
	}
	int Boards::moveMostKill(int coin){
		Coin* virtualCoin;
		int** virtualArea;
		int choice1=0;
		int choice2=0;
		int choice3=0;
		int choice4=0;
		if (isPassAble(coin, 1)){
			virtualArea = copyVirtualArea();
			virtualCoin = copyVirtualCoin();
			passCoinTo(virtualArea, virtualCoin, coin, 1);
			choice1=1+killOneByOne(virtualArea, virtualCoin, coin, virtualCoin[coin-1].getStatus());
			// cout<<endl<<"pilihan 1 = "<<choice1;
			printArea(virtualArea);
		}
		if (isPassAble(coin, 2)){
			virtualArea = copyVirtualArea();
			virtualCoin = copyVirtualCoin();
			passCoinTo(virtualArea, virtualCoin, coin, 2);
			choice2=1+killOneByOne(virtualArea, virtualCoin, coin, virtualCoin[coin-1].getStatus());
			// cout<<endl<<"pilihan 2 = "<<choice2;
			printArea(virtualArea);
		}
		if (isPassAble(coin, 3)){
			virtualArea = copyVirtualArea();
			virtualCoin = copyVirtualCoin();
			passCoinTo(virtualArea, virtualCoin, coin, 3);
			choice3=1+killOneByOne(virtualArea, virtualCoin, coin, virtualCoin[coin-1].getStatus());
			// cout<<endl<<"pilihan 3 = "<<choice3;
			printArea(virtualArea);
		}
		if (isPassAble(coin, 4)){
			virtualArea = copyVirtualArea();
			virtualCoin = copyVirtualCoin();
			passCoinTo(virtualArea, virtualCoin, coin, 4);
			choice4=1+killOneByOne(virtualArea, virtualCoin, coin, virtualCoin[coin-1].getStatus());
			// cout<<endl<<"pilihan 4 = "<<choice4;
			printArea(virtualArea);
		}
		if ((choice1>=choice2) && (choice1>=choice3) && (choice1>=choice4)){
			// cout<<endl<<"pilihan = 1";
			return 1;
		}
		else if ((choice2>=choice1) && (choice2>=choice3) && (choice2>=choice4)){
			// cout<<endl<<"pilihan = 2";
			return 2;
		}
		else if ((choice3>=choice2) && (choice3>=choice1) && (choice3>=choice4)){
			// cout<<endl<<"pilihan = 3";
			return 3;
		}
		else if ((choice4>=choice2) && (choice4>=choice3) && (choice4>=choice1)){
			// cout<<endl<<"pilihan = 4";
			return 4;
		}
		
	}
	int Boards::furthestCoinToMove(int turn){
		int coin = 0;
		int num = 0;
		vector<int> candidate;
		vector<int> kingCandidate;
		// int* candidate;
		// candidate = new int[boardSize/2];
		int startFor, endFor;
		for (int k=0;k<=coinMaxIdx;k++){
			candidate[k] = 0;
		}
		if ((turn ==1)||(turn ==4)){
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
			
		}
		else{
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		std::cout<<endl<<"Himpunan kandidat furthestcointomove antara lain : ";
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) && (ableToMove(i+1))){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
				
				if ((getCoin(i+1).getStatus()==1)){
					if (coin==0){
						coin = i+1;
						std::cout<<endl<<coin;
					}
					else{
						std::cout<<endl<<"Bidak Non Raja: ";
						if ((turn==1) || (turn==4)){ // jika bidak red
							if (getCoin(coin).getY()>getCoin(i+1).getY()){ 
								coin = i+1;
								std::cout<<endl<<coin;
							}						
						}
						else{ // jika bidak black
							if (getCoin(coin).getY()<getCoin(i+1).getY()){ 
								coin = i+1;
								std::cout<<endl<<coin;
							}
						}
					}
				}
				else{
					kingCandidate.push_back(i+1);
				}
			}
		}
		std::cout<<endl<<"Himpunan solusi untuk greedy furthest move antara lain : ";
		for (int j=startFor;j<=endFor;j++){
			if ((getCoin(j+1).getAlive()==1)&& (ableToMove(j+1)) && (getCoin(j+1).getStatus()==1)){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
				if (getCoin(coin).getY()==getCoin(j+1).getY()){
					std::cout<<endl<<"Bidak Non Raja : "<<coin;
					candidate[num] = j+1;
					num+=1;
				}
			}
		}
		
		if (num>1){ // jika banyak pilihan
			// std::cout<<endl<<"num = "<<num;
			int random_integer;
			do{
				// srand((unsigned)time(0));
				random_integer = (rand()%boardSize/2);
				// std::cout<<endl<<"random = "<<random_integer;
			} while (candidate[random_integer]==0); // idx candidate dari 0 sampai biardSize/2 dan menghasilkan 0 jika tidak ada isinya
			coin = candidate[random_integer];
		}
		// std::cout<<endl<<"hasil fungsi coin yang terjauh = "<<coin;
		return coin;
	}
	int Boards::safeCoinToMove(int turn){
		int coin = 0;
		int num = 0;
		// int* candidate;
		vector<int> candidate;
		vector<int> kingCandidate;
		vector<int> solution;
		vector<int> kingSolution;
		// candidate = new int[boardSize/2];
		int startFor, endFor;
		// for (int k=0;k<=coinMaxIdx;k++){
			// candidate[k] = 0;
		// }
		if ((turn ==1)||(turn ==4)){
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
			
		}
		else{
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		cout<<endl<<"himpunan kandidat dari bidak yang dapat digerakkan yaitu :"<<endl;
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) && (ableToMove(i+1))){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
			// if ((getCoin(i+1).getAlive()==1) && (ableToMove(i+1)) &&(getCoin(i+1).getStatus()==1)){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
				cout<<endl<<i+1;
				if(getCoin(i+1).getStatus()==1){
					candidate.push_back(i+1);
					if (coin==0){
						coin = i+1;
					}
					else{
						if ((turn==1) || (turn==4)){ // jika bidak red
							if (getCoin(coin).getY()>getCoin(i+1).getY()) { 
								coin = i+1;
							}
						
						}
						else{ // jika bidak black
							if (getCoin(coin).getY()<getCoin(i+1).getY()) { 
								coin = i+1;
							}
						
						}
					}
				}
				else{
					kingCandidate.push_back(i+1);
					cout<<endl<<"raja ="<<i+1;
				}
			}
		}
		// sorting
		if (candidate.size()>1){
			int max;
			int temp;
			int init;
			int idx;
			for (int i=0;i<candidate.size()-1;i++){
				init = candidate[i];
				max = i;
				for (int j=i+1;j<candidate.size();j++){
					if (getType(coin)==1){
						if (getCoin(candidate[max]).getY()>getCoin(candidate[j]).getY()){
							max = j;
							
						}
					}
					else if (getType(coin)==2){
						if (getCoin(candidate[max]).getY()<getCoin(candidate[j]).getY()){
							max = j;
						}					
					}
				}
				cout<<endl<<"max="<<max;
				cout<<endl<<"candidate i="<<candidate[i];
				if (candidate[max]!=candidate[i]){
					// temp = candidate[i];
					swap(candidate[i], candidate[max]);
				}
			}
		}
		cout<<endl;
		cout<<endl<<"hasil sorting:";
		
		for (int i=0;i<candidate.size();i++){
			cout<<endl<<candidate[i];
		}
		
		cout<<endl<<"Himpunan solusinya Safe Coin To Move antara lain:"<<endl;

		for (int i=0;i<candidate.size();i++){
				if (safeMove(candidate[i])){
					solution.push_back(candidate[i]);
					cout<<endl<<"Bidak Non Raja :"<<candidate[i];
				}
				else{
					
				}
		}
		cout<<endl;
		int shortestKing = 9999;
		int tempKing;
		for (int i=0;i<kingCandidate.size();i++){
			tempKing = shortestSafeDistance(kingCandidate[i]);
			if (shortestKing == 9999){
				shortestKing == tempKing;
				kingSolution.push_back(kingCandidate[i]);
			}
			else{
				if (shortestKing<tempKing){
					kingSolution.push_back(kingCandidate[i]);
					shortestKing = tempKing;
				}
			}
		}
		cout<<endl;
		
		
		if (solution.size()>1){ // jika banyak pilihan
			// std::cout<<endl<<"num = "<<num;
			int random_integer;
			clock_t time;
			// do{
				time=clock();
				srand((unsigned int ) time);
				random_integer = (rand()%(solution.size()));
				std::cout<<endl<<"random = "<<random_integer;
			// } while (candidate[random_integer]==0); // idx candidate dari 0 sampai biardSize/2 dan menghasilkan 0 jika tidak ada isinya
			coin = solution[random_integer];
		}
		else{
			if (solution.size()==1){
				coin = solution[0];
			}
			else{
				if (kingCandidate.size()>0){
					coin = kingCandidate[0];
				}
				else{
					coin = candidate[0];
				}
			}
		}
		std::cout<<endl<<"solusi greedy yang dipilih = "<<coin;
		return coin;
	}

	int Boards::leftMove(int coin){
		int result;
		if (getType(coin)==1){
			if (getCoin(coin).getStatus()==1){
				if (ableToMove(coin,1)){
					result = 1;
				}
				else if (ableToMove(coin,2)){
					result = 2;
				}
			}
			else{
				result = shortestDistanceToKill(coin);
				// kalo Raja???
			}
		}
		else{
			if (getCoin(coin).getStatus()==1){
				if (ableToMove(coin,3)){
					result = 3;
				}
				else if (ableToMove(coin,4)){
					result = 4;
				}
			}
			else{
				result = shortestDistanceToKill(coin);
				// kalo Raja???
			}
			
		}
		return result;
	}
	int Boards::shortestSafeDistance(int coin){
		int typeCoin = getType(coin);
		int distance1, distance2, distance3, distance4;
		bool safe1 = false;
		bool safe2 = false;
		bool safe3 = false;
		bool safe4 = false;
		int posX = getCoin(coin).getX();
		int posY = getCoin(coin).getY();
		int sampleX;
		int sampleY;
		bool found = false;
			if (ableToMove(coin, 1)){
				distance1 = 0;
				if (corner(coin,1)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX-1;
					sampleY = posY-1;
					found = false;
					while ((((sampleX)>0) && ((sampleY)>0)) && (!found)){
						distance1 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe1 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe1 = true;
							}
							
						}
						sampleX-=1;
						sampleY-=1;
						if (((sampleX==0) || (sampleY==0)) && (!found)){
							safe1 = true;
							cout<<endl<<"tembok 1";
						}
					}
				}
			}
			if (ableToMove(coin, 2)){
				distance2 = 0;
				if (corner(coin,2)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX+1;
					sampleY = posY-1;
					found = false;
					while ((((sampleX)<boardMaxIdx) && ((sampleY)>0)) && (!found)){
						distance2 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe2 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe2 = true;
							}
							
						}
						sampleX+=1;
						sampleY-=1;
						if (((sampleX==boardMaxIdx) || (sampleY==0))&&(!found)){
							safe2 = true;
							cout<<endl<<"tembok 2";
						}
					}
				}
			}					
			if (ableToMove(coin, 3)){
				distance3 = 0;
				if (corner(coin,3)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX+1;
					sampleY = posY+1;
					found = false;
					while ((((sampleX)<boardMaxIdx) && ((sampleY)<boardMaxIdx)) && (!found)){
						distance3 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe3 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe3 = true;
							}
							
						}
						sampleX+=1;
						sampleY+=1;
						if (((sampleX==boardMaxIdx) || (sampleY==boardMaxIdx)) && (!found)){
							safe3 = true;
							cout<<endl<<"tembok 3";
						}
					}
				}
			}					
			if (ableToMove(coin, 4)){
				distance4 = 0;
				if (corner(coin,4)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX-1;
					sampleY = posY+1;
					found = false;
					while ((((sampleX)>0) && ((sampleY)<boardMaxIdx)) && (!found)){
						distance4 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe4 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe4 = true;
							}
							
						}
						sampleX-=1;
						sampleY+=1;
						if (((sampleX==0) || (sampleY==boardMaxIdx)) && (!found)){
							safe4 = true;
							cout<<endl<<"tembok 4";
						}
					}
				}
			}					
		bool hunt1 = ((!safe1) && (distance1>2));
		bool hunt2 = ((!safe2) && (distance2>2));
		bool hunt3 = ((!safe3) && (distance3>2));
		bool hunt4 = ((!safe4) && (distance4>2));
		int path =9999;
		int choice =9999;
		if (hunt1){
			path = distance1;
			choice = 1;
		}
		if (hunt2){
			if (path!=9999){
				if (distance2<path){
					path = distance2;
					choice = 2;
				}
			}
			else{
				path = distance2;
				choice = 2;
			}
		}
		if (hunt3){
			if (path!=9999){
				if (distance3<path){
					path = distance3;
					choice =3;
				}
			}
			else{
				path = distance3;
				choice = 3;
				
			}
		}
		if (hunt4){
			if (path!=9999){
				if (distance4<path){
					path = distance4;
					choice = 4;
				}
			}
			else{
				path = distance4;
				choice = 4;
			}
		}
		if (choice == 9999){
			cout<<endl<<"choice = 9999";
			if (safe1){
				choice = 1;
			}
			else if (safe2){
				choice = 2;
			}
			else if (safe3){
				choice = 3;
			}
			else if (safe4){
				choice = 4;
			}
			else if (ableToMove(coin, 1)){
				choice = 1;
			}
			else if (ableToMove(coin, 2)){
				choice = 2;
			}
			else if (ableToMove(coin, 3)){
				choice = 3;
			}
			else if (ableToMove(coin, 4)){
				choice = 4;
			}
		}
		return choice;
	}
	int Boards::mostSafeMove(int coin){
		int result=0;
		vector<int> candidate;
		if (getType(coin)==1){
			if (getCoin(coin).getStatus()==1){
				if (safeMove(coin,1)){
				// if (ableToMove(coin,1)){
					cout<<endl<<"dipilih langkah aman ke kiri";
					result = 1;
					candidate.push_back(1);
				}
				else if  (safeMove(coin,2)){
				// else if (ableToMove(coin,2)){
					cout<<endl<<"dipilih langkah aman ke kanan";
					result = 2;
					candidate.push_back(2);
				}
				else if (ableToMove(coin, 1)){
					result = 1;
					candidate.push_back(1);
				}
				else if (ableToMove(coin, 2)){
					result = 2;
					candidate.push_back(2);
				}
			}
			else{
				// kalo Raja???
				result = shortestSafeDistance(coin);
				candidate.push_back(result);
			}
		}
		else{
			if (getCoin(coin).getStatus()==1){
				if (safeMove(coin,3)){
				// if (ableToMove(coin,3)){
					cout<<endl<<"dipilih langkah aman ke kanan";
					result = 3;
					candidate.push_back(3);
				}
				else if (safeMove(coin,4)){
				// else if (ableToMove(coin,4)){
					cout<<endl<<"dipilih langkah aman ke kiri";
					result = 4;
					candidate.push_back(4);
				}
				else if (ableToMove(coin, 3)){
					result = 3;
					candidate.push_back(3);
				}
				else if (ableToMove(coin, 4)){
					result = 4;
					candidate.push_back(4);
				}
			}
			else{
				// kalo Raja???
				result = shortestSafeDistance(coin);
				candidate.push_back(result);
			}
			
		}
		if (candidate.size()>1){ // jika banyak pilihan
			int random_integer;
			clock_t time;
			time=clock();
			srand((unsigned int ) time);
			random_integer = (rand()%(candidate.size()));
			// std::cout<<endl<<"random = "<<random_integer;
			result = candidate[random_integer];
		}
		else if (candidate.size()==1){
			result = candidate[0];
		}
		if (result==0){
			cout<<endl<<"fungsi safe move error";
		}
		return result;
	}

	int Boards::shortestDistanceToKill(int coin){
		int typeCoin = getType(coin);
		int distance1=0; 
		int distance2=0;
		int distance3=0;
		int distance4=0;
		bool safe1 = false;
		bool safe2 = false;
		bool safe3 = false;
		bool safe4 = false;
		int posX = getCoin(coin).getX();
		int posY = getCoin(coin).getY();
		int sampleX;
		int sampleY;
		bool found = false;
			if (ableToMove(coin, 1)){
				distance1 = 0;
				if (corner(coin,1)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX-1;
					sampleY = posY-1;
					found = false;
					while ((((sampleX)>0) && ((sampleY)>0)) && (!found)){
						distance1 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe1 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe1 = true;
							}
							
						}
						sampleX-=1;
						sampleY-=1;
						if (((sampleX==0) || (sampleY==0)) && (!found)){
							safe1 = true;
							cout<<endl<<"tembok 1";
						}
					}
				}
			}
			if (ableToMove(coin, 2)){
				distance2 = 0;
				if (corner(coin,2)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX+1;
					sampleY = posY-1;
					found = false;
					while ((((sampleX)<boardMaxIdx) && ((sampleY)>0)) && (!found)){
						distance2 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe2 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe2 = true;
							}
							
						}
						sampleX+=1;
						sampleY-=1;
						if (((sampleX==boardMaxIdx) || (sampleY==0))&&(!found)){
							safe2 = true;
							cout<<endl<<"tembok 2";
						}
					}
				}
			}					
			if (ableToMove(coin, 3)){
				distance3 = 0;
				if (corner(coin,3)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX+1;
					sampleY = posY+1;
					found = false;
					while ((((sampleX)<boardMaxIdx) && ((sampleY)<boardMaxIdx)) && (!found)){
						distance3 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe3 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe3 = true;
							}
							
						}
						sampleX+=1;
						sampleY+=1;
						if (((sampleX==boardMaxIdx) || (sampleY==boardMaxIdx)) && (!found)){
							safe3 = true;
							cout<<endl<<"tembok 3";
						}
					}
				}
			}					
			if (ableToMove(coin, 4)){
				distance4 = 0;
				if (corner(coin,4)){ // bila coin berada di sudut
					// cout<<endl<<"udah di ujung papan, jadi safe";
				}
				else{
					sampleX = posX-1;
					sampleY = posY+1;
					found = false;
					while ((((sampleX)>0) && ((sampleY)<boardMaxIdx)) && (!found)){
						distance4 +=1;
						if (area[sampleY][sampleX]!=0){
							found = true;
							if (typeCoin!=getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe4 = false;
							}
							if (typeCoin==getType(getCoinNumberFromArea(sampleX, sampleY))){
								safe4 = true;
							}
							
						}
						sampleX-=1;
						sampleY+=1;
						if (((sampleX==0) || (sampleY==boardMaxIdx)) && (!found)){
							safe4 = true;
							cout<<endl<<"tembok 4";
						}
					}
				}
			}					
		bool hunt1 = (distance1>2);
		bool hunt2 =  (distance2>2);
		bool hunt3 =  (distance3>2);
		bool hunt4 =  (distance4>2);
		int path =9999;
		int choice =9999;
		if (hunt1){
			path = distance1;
			choice = 1;
			cout<<endl<<"hunt1 ";
		}
		if (hunt2){
			if (path!=9999){
				if (distance2<path){
					path = distance2;
					choice = 2;
					cout<<endl<<"hunt2 ";
				}
			}
			else{
				path = distance2;
				choice = 2;
			}
		}
		if (hunt3){
			if (path!=9999){
				if (distance3<path){
					path = distance3;
					cout<<endl<<"hunt3 ";
					choice =3;
				}
			}
			else{
				path = distance3;
				choice = 3;
				cout<<endl<<"hunt4 ";
				
			}
		}
		if (hunt4){
			if (path!=9999){
				if (distance4<path){
					path = distance4;
					choice = 4;
				}
			}
			else{
				path = distance4;
				choice = 4;
			}
		}
		path = 9999;
		if (choice == 9999){
			cout<<endl<<"choice = 9999";
			if (safe1){
				path = distance1;
				choice = 1;
			}
			else if (safe2){
				if (distance2<path){
					choice = 2;
					path = distance2;
				}
			}
			else if (safe3){
				if (distance3<path){
					choice = 3;
					path = distance3;
				}
			}
			else if (safe4){
				if (distance4<path){
					choice = 4;
					path = distance4;
				}
			}
			else if (ableToMove(coin, 1)){
				choice = 1;
			}
			else if (ableToMove(coin, 2)){
				choice = 2;
			}
			else if (ableToMove(coin, 3)){
				choice = 3;
			}
			else if (ableToMove(coin, 4)){
				choice = 4;
			}
		}
		cout<<endl<<"choice shortestdistance = "<<choice;
		return choice;
	}
	int Boards::furthestCoinToKill(int turn){
		int strong=0;
		int choice=0;
		int num = 0;
		int* candidate; // for candidate function
		int startFor, endFor; // for iteration

		if ((turn ==1)||(turn ==4)){ // if coin is red
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
			
		}
		else{ // if coin is black
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		// re-init value of candidate
		num=0;
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) &&(checkArea(i+1)!=0)){
				// candidate[num] = i+1; // assign value of candidate
				if (choice ==0 ){ // if it's the first time can eat
					choice = i+1;
					// std::cout<<endl<<"coin pertama to kill = "<<choice;
				}else{
					if ((turn ==1)||(turn ==4)){ // turn is red
						if (getCoin(choice).getY() > getCoin(i+1).getY()){
							choice = i+1;
						}					
					}
					else{
						if (getCoin(choice).getY() < getCoin(i+1).getY()){
							choice = i+1;
						}									
					}
				}
			}
		}

		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) &&(checkArea(i+1)!=0) && (getCoin(choice).getY() == getCoin(i+1).getY())){
				num+=1;
			}
		}
		// candidate coin is ready
		// std::cout<<endl<<"num"<<num;

		if (num>1){ // jika banyak pilihan 
			int iterasi=0;
			int random_integer;
			// init candidate function
			// init size of candidate
			candidate = new int[num];
			for (int k=0;k<num;k++){
				candidate[k] = 0;
			}		
			for (int i=startFor;i<=endFor;i++){
				if ((getCoin(i+1).getAlive()==1) &&(checkArea(i+1)!=0) && (getCoin(choice).getY() == getCoin(i+1).getY())){
					candidate[iterasi] = i+1;
					iterasi+=1;
				}
			}
			do{
				// srand((unsigned)time(0));
				random_integer = (rand()%num);
			} while (candidate[random_integer]==0); // idx candidate dari 0 sampai biardSize/2 dan menghasilkan 0 jika tidak ada isinya
			choice = candidate[random_integer];
			// std::cout<<endl<<"hasil coin random = "<< choice;

		}
		else{
			// std::cout<<endl<<"cuma satu yang bisa makan";
		}
		// std::cout<<endl<<"hasil fungsi coin yang paling depan untuk makan = "<<choice;
		if (choice ==0){
			cout<<"coin further coin to kill error";
		}
		return choice;
	
	}
	int Boards::leftKill(int coin){
		int result =0 ;
		if (getType(coin)==1){
			if (getCoin(coin).getStatus()==1){
				if (isPassAble(coin,1)){
					result = 1;
				}
				else if (isPassAble(coin,2)){
					result = 2;
				}
			}
			else{
				// kalo Raja???
			}
		}
		else{
			if (getCoin(coin).getStatus()==1){
				if (isPassAble(coin,3)){
					result = 3;
				}
				else if (isPassAble(coin,4)){
					result = 4;
				}
			}
			else{
				// kalo Raja???
			}
			
		}
		if (result == 0){
			cout<<endl<<"error left kill";
		}
		return result;
	}
	bool Boards::moreChanceToMove(int currentCoin){
		int able = 0;
		
			if (ableToMove(currentCoin,1)){
					able +=1;
			}
			if (ableToMove(currentCoin,2)){
					able +=1;
			}
			if (ableToMove(currentCoin,3)){
					able +=1;
			}
			if (ableToMove(currentCoin,4)){
					able +=1;
			}
		if (able>1){
			return true;
		}
		else{
			return false;
		}
	}
	void Boards::setCoin(Coin *mCoin){
		myCoin = new Coin [coinMaxIdx+1] ;
		for (int k =0; k<=coinMaxIdx; k++){
			myCoin[k].setX(mCoin[k].getX());
			myCoin[k].setY(mCoin[k].getY());
			myCoin[k].setNumber(k+1);
			myCoin[k].setStatus(mCoin[k].getStatus());			
			myCoin[k].setAlive(mCoin[k].getAlive());			
		}

	}
	Coin* Boards::allCoin(){
		return myCoin;
	}
	int Boards::remainingCoin(int bidak){
		int startFor, endFor;
		int alive =0;
		if (bidak == 1){ // 1 untuk red
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
		}
		else{
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		for (int i=startFor;i<=endFor;i++){
			if (getCoin(i+1).getAlive()==1){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
				alive+=1;
			}
		}
		return alive;	
	}
	bool Boards::noCoinCanMove(int bidak){
		int startFor, endFor;
		bool check = true;
		if (bidak==1){ // 1 untuk bidak red
			startFor = secondCoinStartIdx;
			endFor = coinMaxIdx;
			
		}
		else{
			startFor = coinMinIdx;
			endFor = (secondCoinStartIdx-1);
		}
		for (int i=startFor;i<=endFor;i++){
			if ((getCoin(i+1).getAlive()==1) && ((ableToMove(i+1)) || (isPassAble(i+1)))){ // jika bidak yang masih hidup dan dapat bergerak dan bukan raja
				check = false;
			}
		}
		return check;

	}
	
//---------------------------------------------------------------------------

